package com.tianyang.yukuaifu.business.main.presenter;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifu.business.main.bean.SettleOutput;
import com.tianyang.yukuaifu.business.main.contract.SettleManageContract;
import com.tianyang.yukuaifu.business.trade.model.TradeModel;
import com.tianyang.yukuaifu.module.dialog.HintDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.OnBtnClickL;
import com.tianyang.yukuaifu.utils.IntentTool;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：结算管理
 * Created by xyj on 2024/11/19
 */
public class SettleManagePresenter implements SettleManageContract.Presenter {

    private SettleManageContract.View mView;

    private CompositeSubscription mSubscription;

    public SettleManagePresenter(SettleManageContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void updateMemberSettleCycle(String settleCycle, String auditTime) {
        mView.showLoadingDialog(false);
        Subscription subscription = TradeModel.getInstance()
                .updateMemberSettleCycle(settleCycle, auditTime)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(JSONObject output) {
                        new HintDialog(mView.getContext())
                                .title("温馨提示")
                                .content("结算周期设置成功")
                                .listener(new OnBtnClickL() {
                                    @Override
                                    public void onBtnClick() {
                                        IntentTool.finishActivity(mView.getContext());
                                    }
                                })
                                .show();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void getSettleData() {
        List<SettleOutput> outputs = new ArrayList<>();
        outputs.add(new SettleOutput("D0结算", "0"));
        outputs.add(new SettleOutput("D1结算", "1"));
        outputs.add(new SettleOutput("T0结算", "2"));
        outputs.add(new SettleOutput("T1结算", "3"));
        outputs.add(new SettleOutput("定时结算", "4"));
        mView.showSettleView(outputs);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
